package com.wn.class30.cotroller.houtai;

import com.wn.class30.entity.Producttype;
import com.wn.class30.service.BackProducttypeService;
import com.wn.class30.tools.Page;
import com.wn.class30.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class BackProducttypeController {
    @Resource
    private BackProducttypeService backProducttypeService;

    @RequestMapping("/houtai/page/products/producttype/list")
    @RequiresPermissions("/houtai/page/products/producttype/list")
    public Result getList()throws Exception{
        return new Result("200","success",null,null);
    }
    @RequestMapping("/houtai/products/getProducttypes")
    public Result getProductsByPage(Integer page,Integer limit,String name) throws Exception{
        Map<String,String> map = new HashMap<>();
        map.put("page",String.valueOf(page));
        map.put("limit",String.valueOf(limit) );
        map.put("name", name);
        Page pager = new Page();
        pager.setCurrentPage(page);
        pager.setPageSize(limit);
        int count = backProducttypeService.selectCount((HashMap) map);
        pager.setTotalSize(count);
        pager.setTotalPage(count%limit==0?count/limit:count/limit+1);
        return new Result("0","success",pager, (List) backProducttypeService.select((HashMap) map));
    }
    @RequestMapping("/houtai/page/products/producttype/delete")
    @RequiresPermissions("/houtai/page/products/producttype/delete")
    public Result deleteProducttype(@RequestParam("producttypeIds[]") Integer[] producttypeIds)throws Exception{

        for (Integer producttypeId : producttypeIds) {
            backProducttypeService.deleteById(producttypeId);
        }

        return new Result("0","success",null, null);
    }
    @RequestMapping("/houtai/page/products/producttype/update")
    @RequiresPermissions("/houtai/page/products/producttype/update")
    public Result updateProducttype(Producttype producttype)throws Exception{
        backProducttypeService.updateProducttype(producttype);
        return new Result("0","success",null, null);
    }
    @RequestMapping("/houtai/page/products/producttype/insert")
    @RequiresPermissions("/houtai/page/products/producttype/insert")
    public Result insertProducttype(Producttype producttype)throws Exception{

        backProducttypeService.insertProducttype(producttype);
        return new Result("0","success",null, null);
    }
}
